package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    //查询当前页套餐
    @GetMapping("/setmeal/page")
    public ResultInfo getSetmealPage(
            String name,
            @RequestParam(value = "page" ,defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize" , defaultValue = "10") Integer pageSize){

        Page<Setmeal> page = setmealService.getSetmealPage(name,pageNum,pageSize);
        return ResultInfo.success(page);
    }
    @CacheEvict(value = "setmeal" , key = "#setmeal.categoryId")
    //保存
    @PostMapping("/setmeal")
    public ResultInfo save(@RequestBody Setmeal setmeal){
        //调用方法
        setmealService.save(setmeal);
        return ResultInfo.success(null);
    }
    //修改前回显数据
    @GetMapping("/setmeal/{id}")
    public ResultInfo findById(@PathVariable("id")Long id){
        Setmeal setmeal = setmealService.findById(id);
        return ResultInfo.success(setmeal);
    }
    @CacheEvict(value = "setmeal" , key = "#setmeal.categoryId")
    //修改
    @PutMapping("/setmeal")
    public ResultInfo updateSetmeal(@RequestBody Setmeal setmeal){
        //调用方法
        setmealService.updateSetmeal(setmeal);
        return ResultInfo.success(null);
    }
    @CacheEvict(value = "setmeal" , allEntries = true)
    //删除
    @DeleteMapping("/setmeal")
    public ResultInfo delete(Long[] ids){

        setmealService.delete(ids);

        return ResultInfo.success(null);
    }
    @CacheEvict(value = "setmeal" , allEntries = true)
    //起售停售
    @PostMapping("/setmeal/status/{status}")
    public ResultInfo status(@PathVariable("status") Integer status ,Long[] ids){

        setmealService.status(status,ids);

        return ResultInfo.success(null);
    }

    //查找套餐
    @CacheEvict(value = "setmeal" , allEntries = true)
    @GetMapping("/setmeal/list")
    public ResultInfo getSetmeal(Long categoryId,Integer status){

        List<Setmeal> setmealLish = setmealService.getSetmeal(categoryId,status);
        return ResultInfo.success(setmealLish);
    }

    //查询套餐中的dish
    @GetMapping("/setmeal/dish/{id}")
    public ResultInfo getSetmealDish(@PathVariable("id") Long id){
        List<SetmealDish> dishList = setmealService.getSetmealDish(id);
        return ResultInfo.success(dishList);
    }
}
